package com.feel.service.impl;

import com.feel.exception.SteduException;
import com.feel.mapper.AppointmentMapper;
import com.feel.mapper.DepartmentMapper;
import com.feel.mapper.UserMapper;
import com.feel.pojo.Appointment;
import com.feel.pojo.Department;
import com.feel.pojo.User;
import com.feel.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 刘永钦
 * @Date: 2024-09-03-14:11
 * @Description:
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;


    @Override
    @Cacheable(value = "home_appliance", key = "'department:list'", unless = "#result == null || #result.size() == 0")
    public List<Department> getAll() {
        return departmentMapper.getAll();
    }

    @Override
    @CacheEvict(value = "home_appliance", key = "'department:list'")
    public Boolean addInfo(Department department) throws SteduException {
        Department byName = departmentMapper.getByName(department.getDepartmentName());
        if (byName != null) {
            throw  new SteduException("业务名称存在，请重新输入");
        }
        Integer i = departmentMapper.addInfo(department);
        return i > 0;
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "home_appliance", key = "'department:list'"),
            @CacheEvict(value = "home_appliance", key = "'dapartment:' + #id")
    })
    public Boolean delInfo(Integer id) throws SteduException {
        // todo 判断是否还存在人员和或者订单
        Integer departNumber = userMapper.getDepartNumber(id);
        if (departNumber != null){
            throw new SteduException("还有人员从事该业务");
        }
        // todo 订单判断

        Integer i = departmentMapper.delInfo(id);
        return i > 0;
    }

    @Override
    @Caching(evict = {
            @CacheEvict(value = "home_appliance", key = "'department:list'"),
            @CacheEvict(value = "home_appliance", key = "'dapartment:' + #department.departmentId")
    })
    public Boolean updateInfo(Department department) throws SteduException {
        // 判断是否重名
        Department byName = departmentMapper.getByName(department.getDepartmentName());
        if (byName != null) {
            throw new SteduException("业务名称重复，请重新输入");
        }
        Integer i = departmentMapper.updateInfo(department);
        return i > 0;
    }

    @Override
    @Cacheable(value = "home_appliance", key = "'dapartment:' + #id",unless = "#result == null ")
    public Department getByID(Integer id) {
        Department department = departmentMapper.getById(id);
        return  department;
    }

    @Override
    public List<Map<String, Object>> getDepartmentEcharts() {
        List<Department> list = departmentMapper.getAll();
        List<Map<String, Object>> mapList = list.stream()
                .map(item -> {
                    Map<String, Object> map = new HashMap<>();
                    Integer number = userMapper.getDepartNumber(item.getDepartmentId());
                    map.put("value", number);
                    map.put("name", item.getDepartmentName());
                    return map;
                })
                .collect(Collectors.toList());
        return mapList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateAdmin(Integer oldId, Integer userId) throws SteduException {
        Appointment appointment = new Appointment();
        appointment.setEmpId(userId);
        appointment.setAppointmentStatus(2);
        List<Appointment> list = appointmentMapper.getAll(appointment);
        if (list != null && list.size() > 0) {
            throw new SteduException("员工存在未完成订单，请处理完成后再任职！！！");
        }

        User oldUser = new User();
        oldUser.setUserId(oldId);
        oldUser.setRole("emp");

        Integer i = userMapper.updateUser(oldUser);

        User newUser = new User();
        newUser.setUserId(userId);
        newUser.setRole("admin");

        Integer i1 = userMapper.updateUser(newUser);
        return i + i1 > 0;
    }
}
